﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Кулик
 * Дата: 29.03.2011
 * Время: 16:51
 * 
 * Для изменения этого шаблона используйте Сервис | Настройка | Кодирование | Правка стандартных заголовков.
 */
using System;
using System.IO;
using NLog;

namespace Video.Mov
{
	
	public class Encoder
	{
		Logger log;
		VideoStream videoStream;
		
		public Encoder(string outFilePath)
		{
			videoStream = new VideoStream(outFilePath,true);
			log =  LogManager.GetCurrentClassLogger();
		}
		
		public void WriteAtom(Atom atom){
			videoStream.avio_wb32(atom.Size);
			videoStream.outVideoStream.Write(atom.Type);
			string type = new String(atom.Type);
			switch (type) {
				case "ftyp":
					WriteFtyp((FtypAtom)atom);
					break;
				case "mvhd":
					WriteMvhd((MovieHeaderAtom)atom);
					break;
				default:
					
					break;
			}
			
			foreach (var item in atom.Child) {
				WriteAtom(item);
			}
		}
		
		public void WriteFtyp(FtypAtom atom){
			//videoStream.avio_wb32(atom.Size);
			//videoStream.outVideoStream.Write(atom.Type);
			
			videoStream.outVideoStream.Write(atom.MajorBrand);
			videoStream.avio_wb32(atom.Ver);
			videoStream.outVideoStream.Write(atom.CompatibleBand1);
			videoStream.outVideoStream.Write(atom.CompatibleBand2);
		}		
		
		public void WriteMvhd(MovieHeaderAtom atom){	
			//videoStream.avio_wb32(atom.Size);
			//videoStream.outVideoStream.Write(atom.Type);
			
			videoStream.avio_w8(atom.Version);
			videoStream.outVideoStream.Write(atom.Flags);
			videoStream.avio_wb32(atom.CreationTime);
			videoStream.avio_wb32(atom.ModificationTime);
			videoStream.avio_wb32(atom.TimeScale);
			videoStream.avio_wb32(atom.Duration);
			videoStream.avio_wb32(atom.PreferredRate);
			videoStream.avio_wb16(atom.PreferredVolume);
			videoStream.outVideoStream.Write(atom.Reserved);
			videoStream.outVideoStream.Write(atom.MatrixStructure);
			videoStream.avio_wb32(atom.PreviewTime);
			videoStream.avio_wb32(atom.PreviewDuration);
			videoStream.avio_wb32(atom.PosterTime);
			videoStream.avio_wb32(atom.SelectionTime);
			videoStream.avio_wb32(atom.SelectionDuration);
			videoStream.avio_wb32(atom.CurrentTime);
			videoStream.avio_wb32(atom.NextTrackID);
		}
		
	}
}
